package com.zzyl.serve.service.impl;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.dto.CancelTaskDto;
import com.zzyl.nursing.dto.ExecuteTaskDto;
import com.zzyl.nursing.dto.UpdateTaskTimeDto;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.vo.NursingTaskByIdVo;
import com.zzyl.serve.domain.NursingTaskDto;
import com.zzyl.serve.domain.NursingTaskVo;
import com.zzyl.serve.mapper.NursingFvkMapper;
import com.zzyl.serve.service.INursingFvkService;
import com.zzyl.system.mapper.SysUserMapper;
import com.zzyl.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @Date 2025/10/29 16:17
 */
@Service
public class NursingFvkServiceImpl extends ServiceImpl<NursingFvkMapper, NursingTask> implements INursingFvkService {

    @Autowired
    private NursingFvkMapper nursingFvkMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ElderMapper elderMapper;

    /**
     * 2.2.1 查询护理任务列表
     *
     * @param dto
     * @return
     */
/*    @Override
    public List<NursingTask> getNursingFvkTableData(QueryNursingTaskfvkDto dto) {
        //查找护理任务表数据
        List<NursingTask> list = this.lambdaQuery().like(dto.getElderName() != null, NursingTask::getElderName, dto.getElderName())
                //.eq(dto.getNurseId() != null, NursingTask::getNursingId, dto.getNurseId())
                .eq(dto.getProjectId() != null, NursingTask::getProjectId, dto.getProjectId())
                .eq(dto.getStatus()!=null, NursingTask::getStatus, dto.getStatus())
                .eq(dto.getNurseId()!=null,NursingTask::getNurseId,dto.getNurseId())
                .between(dto.getStartTime() != null && dto.getEndTime() != null, NursingTask::getEstimatedServerTime, dto.getStartTime(), dto.getEndTime())
                .list();
        //遍历数据,切割成单个id查询

        for (NursingTask nursingTask : list) {
            String[] split = nursingTask.getNursingId().split(",");
            for (String s : split) {
            }
            //准备接收查出来的姓名
            String[] nameStr = new String[nursingTask.getNursingId().split(",").length];
            for (int i = 0; i < split.length; i++) {
                Long id = Long.valueOf(split[i]);
                //查询护理员姓名
                SysUser user = sysUserMapper.selectUserById(id);
                //塞入数组中
                nameStr[i] = user.getNickName();
            }
            //塞入集合中
            nursingTask.setNursingName(nameStr);
        }
        //根据前端传来NurseId查询护理员姓名
*//*        if (dto.getNurseId()!=null) {
            Long[] nurseId = dto.getNurseId();
            String[] arr = new String[nurseId.length];
            for (int i = 0; i < nurseId.length; i++) {
                for (Long id : nurseId) {
                    SysUser user = sysUserMapper.selectUserById(id);
                    arr[i]=user.getNickName();
                }
            }
        }*//*
        return list;
    }*/
    @Override
    public List<NursingTaskVo> selectNursingTaskList(NursingTaskDto dto)
    {
        List<NursingTaskVo> nursingTaskVos = nursingFvkMapper.pageList(dto);
        for (NursingTaskVo nursingTaskVo : nursingTaskVos) {
            //设置护理人员姓名，切割护理Id
            List<String> nursingIds = StrUtil.split(nursingTaskVo.getNursingId(), ',');
            List<String> names = new ArrayList<>();
            for (String nursingId : nursingIds) {
                SysUser sysUser = sysUserService.selectUserById(Long.valueOf(nursingId));
                names.add(sysUser.getNickName());
            }
            nursingTaskVo.setNursingName(names);
        }
        return nursingTaskVos;
    }

    /**
     * 根据id查询详细信息
     *
     * @param id
     * @return
     */
    @Override
    public NursingTaskByIdVo getDetailDataById(Integer id) {
        //根据sql语句查找查询 护理任务表, 老人的姓名以及性别,身份证号
        NursingTaskByIdVo nursingTaskByIdVo=nursingFvkMapper.selectBuSql(id);
        //将查出来的String类型id进行切割
        String[] split = nursingTaskByIdVo.getNursingId().split(",");
        //创建集合准备接收姓名
        String[] nameStr = new String[split.length];
        //遍历集合id  将查出来的护理员姓名塞入姓名集合中
        for (int i = 0; i < split.length; i++) {
            SysUser nursing = sysUserMapper.selectUserById(Long.valueOf(split[i]));
            nameStr[i]=nursing.getNickName();
        }
        //将姓名集合塞入Vo中
        nursingTaskByIdVo.setNursingName(nameStr);
        //根据老人身份证获取年龄
        nursingTaskByIdVo.setAge(IdcardUtil.getAgeByIdCard(nursingTaskByIdVo.getIdCardNo()));
        nursingTaskByIdVo.setRemark("仔细"+nursingTaskByIdVo.getProjectName());
//        nursingTaskByIdVo.setSex(String.valueOf(IdcardUtil.getGenderByIdCard(nursingTaskByIdVo.getIdCardNo())==1?null:0));
        if (IdcardUtil.getGenderByIdCard(nursingTaskByIdVo.getIdCardNo())==1) {
            nursingTaskByIdVo.setSex(null);
        }else {
            nursingTaskByIdVo.setSex("1");
        }
        return nursingTaskByIdVo;
    }

    /**
     * 取消任务
     *
     * @param dto
     */
    @Override
    public void cancelTaskById(CancelTaskDto dto) {
        this.lambdaUpdate().eq(dto.getTaskId()!=null,NursingTask::getId, dto.getTaskId())
                .set(NursingTask::getCancelReason, dto.getReason())
                .set(NursingTask::getStatus, 3)
                .set(NursingTask::getUpdateTime, LocalDateTime.now())
                .set(NursingTask::getUpdater, sysUserMapper.selectUserById(SecurityUtils.getUserId()).getNickName())
                .update();

    }

    /**
     * 任务执行
     *
     * @param dto
     */
    @Override
    public void executeTask(ExecuteTaskDto dto) {
        this.lambdaUpdate().eq(dto.getTaskId()!=null,NursingTask::getId, dto.getTaskId())
                .set(NursingTask::getEstimatedServerTime, dto.getEstimatedServerTime())
                .set(NursingTask::getMark, dto.getMark())
                .set(NursingTask::getTaskImage, dto.getTaskImage())
                .set(NursingTask::getStatus, 2)
                .set(NursingTask::getRealServerTime, LocalDateTime.now())
                .set(NursingTask::getUpdater, sysUserMapper.selectUserById(1L).getNickName())
                .update();

    }

    /**
     * 任务该时间
     *
     * @param dto
     */
    @Override
    public void updateTaskTime(UpdateTaskTimeDto dto) {
        this.lambdaUpdate().eq(dto.getTaskId()!=null,NursingTask::getId, dto.getTaskId())
                .set(NursingTask::getEstimatedServerTime, dto.getEstimatedServerTime())
                .update();
    }

}
